package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.Menu;
import com.itheima.service.MenuService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author W.J.Y
 */
@RestController
@RequestMapping("menu")
public class MenuController {

    @Reference(version = "1.0.0")
    private MenuService menuService;

    /**
     * 新增菜单
     * @param menu  菜单实例
     * @return  统一返回结果
     */
    @RequestMapping("add")
    @PreAuthorize("hasAuthority('MENU_ADD')")
    public Result add(@RequestBody Menu menu){
        //调用业务层添加方法
        menuService.add(menu);
        //返回操作结果
        return new Result(true, "新增菜单成功！");
    }


    /**
     * 编辑菜单
     * @param menu  用户菜单
     * @return  统一返回结果
     */
    @RequestMapping("update")
    @PreAuthorize("hasAuthority('MENU_EDIT')")
    public Result update(@RequestBody Menu menu){
        //调用业务层修改方法
        menuService.update(menu);
        //返回操作结果
        return new Result(true, "修改菜单成功！");
    }

    /**
     * 根据ID删除菜单
     * @param id    菜单ID
     * @return  返回结果
     */
    @GetMapping("delete/{id}")
    @PreAuthorize("hasAuthority('ROLE_DELETE')")
    public Result delete(@PathVariable int id){
        //调用业务层删除方法
        menuService.delete(id);
        //返回操作结果
        return new Result(true, "菜单删除成功！");
    }

    /**
     * 根据ID查询菜单信息
     * @param id    菜单ID
     * @return  返回结果
     */
    @RequestMapping("findById/{id}")
    public Result findById(@PathVariable int id){
        Menu menu = menuService.findById(id);
        return new Result(true,"查询菜单信息成功！",menu);
    }

    /**
     * 分页查询菜单数据
     * @param queryPageBean 分页查询语句
     * @return  返回结果
     */
    @RequestMapping("findPage")
    @PreAuthorize("hasAuthority('MENU_QUERY')")
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean){
        PageResult pageResult = menuService.findPage(
                queryPageBean.getCurrentPage(),
                queryPageBean.getPageSize(),
                queryPageBean.getQueryString());
        return pageResult;
    }

    /**
     * 查询全部菜单信息
     * @return  全部菜单
     */
    @RequestMapping("findByLevel/{level}")
    @PreAuthorize("hasAuthority('MENU_QUERY')")
    public Result findByLevel(@PathVariable int level){
        List<Menu> all = menuService.findByLevel(level);
        return new Result(true,"查询菜单成功！",all);
    }

    /**
     * 查询全部菜单信息
     * @return  全部菜单信息
     */
    @RequestMapping("findAll")
    @PreAuthorize("hasAuthority('MENU_QUERY')")
    public Result findAll(){
        List<Menu> all = menuService.findAll();
        return new Result(true,"查询全部菜单成功",all);
    }
}